# -*- encoding: utf-8 -*- 
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import unittest, time
#import Tkinter,os

class BaiduSpace(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "http://hi.baidu.com"
        self.verificationErrors = []
    
    def test_baiduSpace(self):
        driver = self.driver
        driver.get(self.base_url + "/go/login")
        driver.find_element_by_id("username").click()
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("zhengchunxi@foxmail.com")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("testMiaozhen")

        driver.find_element_by_css_selector("div.mod-guide-form-input.mod-guide-form-input-12").click()
        driver.find_element_by_id("memPass").click()
        driver.find_element_by_xpath("//form[@id='formLogin']/div/div/div/div[3]/div[5]/div[3]/a/span[2]").click()
        driver.find_element_by_css_selector("div.ent-tip").click()
        titles = driver.window_handles       #拿到句柄
        driver.find_element_by_id("title").click()  #拿到title输入框
        driver.find_element_by_id("title").clear()
        driver.find_element_by_id("title").send_keys("Test")
        driver.find_element_by_css_selector("#edui31_body > div.edui-box.edui-icon").click()  #拿到插入图片标签
        openFile = driver.find_element_by_css_selector("#edui29_body > div.edui-box.edui-label") #拿到内容输入框
        openFile.click()
        print openFile  
        driver.switch_to_frame("edui27_iframe")
        driver.find_element_by_xpath("//div[@id='picTab']/span[1]/a").click()
        time.sleep(2)
        driver.find_element_by_xpath("//div[@id=\"picUpFlash\"]/object").click()   #拿到【添加图片】，点击后可选择文件
        print '打开预览框'
        time.sleep(2)
        
#        implement = driver.find_element_by_id("picUpFlash").click()
#        os.system('autoit.exe') 
#
#        def RunExe(self):
#            os.popen('autoit.exe')
#            
#        win = Tkinter.Tk() 
#        b = Tkinter.Button(win, text='Hello world!', command=RunExe)
#        b.pack() 
#        win.mainloop()
#        print "b"
#        time.sleep(3)
#    
#        file_path = os.path.abspath("C:\\b.jpg")
#        print "c"
#        time.sleep(5)
#        implement.send_keys(file_path)
        
#        driver.find_element_by_xpath("//div[@id='edui29_body']/div[2]").click()
#        driver.find_element_by_id("qPubBtnSubmit").click()
#        driver.find_element_by_id("qNavRightExit").click()
#        driver.find_element_by_xpath("(//input[@type='text'])[2]").clear()
#        driver.find_element_by_xpath("(//input[@type='text'])[2]").send_keys("http://postimg1.mop.com/2011/04/05/13020007455863507.jpg")
#        driver.find_element_by_css_selector("#edui29_body > div.edui-box.edui-label").click()
       
        driver.switch_to_window(titles)   #切换窗口句柄
                
        configBtn = driver.find_elements_by_xpath("//div[@id='edui29_body']/div[2]")  #上传文件后点击【确认】
        print configBtn
        driver.find_element_by_css_selector("body").click()
        driver.find_element_by_id("qPubBtnSubmit").click()
        driver.find_element_by_id("qNavRightExit").click()
    
    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException: return False
        return True
    
    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()
